﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "curl_impl.h"
#include "mgr_impl.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_curl
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    mgr_impl::mgr_impl()
    {
        m_bInit = false;
    }

    mgr_impl::~mgr_impl()
    {
        if( g_pSysData )
        {
            g_pSysData->term();
            delete g_pSysData;
            g_pSysData = 0;
        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    // 
    // create node
    // 
    int mgr_impl::create( i_curl ** ppv )
    {
        int nRet = 0;

        curl_impl * p = curl_impl::get_item_from_pool( true );
        p->init();
        *ppv = p;

        return nRet;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int mgr_impl::put_back_to_pool( T * pT, bool bLock )
    {
        int ret = 0;
        delete this;
        return ret;
    }

    // 
    // init
    // 
    int mgr_impl::init()
    {
        int nResult = -1;
        int nRet = 0;

        if( 0 == nRet )
        {
            if( m_bInit )
            {
                nRet = 1;
            }
        }

        if( 0 == nRet )
        {
            nRet = curl_global_init( CURL_GLOBAL_ALL );
        }

        if( 0 == nRet )
        {
            m_bInit = true;
            nResult = 0;
        }

        return nResult;
    }

    // 
    // term
    // 
    int mgr_impl::term()
    {
        int nRet = 0;

        if( m_bInit )
        {
            curl_global_cleanup();
            m_bInit = false;
        }

        return nRet;
    }

} // xos_curl
